//
//  CYWinningTicketCell.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/6/9.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYWinningTicketCell.h"

@interface CYWinningTicketCell()
@property (nonatomic, strong) CYLabel *typeLBL;//彩种
@property (nonatomic, strong) CYLabel *dateLBL;//日期
@property (nonatomic, strong) CYLabel *timeLBL;//时间
@property (nonatomic, strong) CYLabel *tipsLBL;//正在开奖
@property (nonatomic, strong) UIImageView *dateIMG;//日期图片
@property (nonatomic, strong) CYView *greenView;//快三系列绿色背景
@property (nonatomic, strong) CYLabel *sumLBL;//快三系列和值

@end
@implementation CYWinningTicketCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        [self setSeparatorInset:UIEdgeInsetsZero]; /*cell的分割线，左对齐*/
        [self setLayoutMargins:UIEdgeInsetsZero];  /*cell的分割线，左对齐*/
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    return self;
}

/*懒加载*/
- (CYLabel *)typeLBL{
    if (!_typeLBL) {
        _typeLBL = [[CYLabel alloc] initWithText:_name size:14 textcolor:ColorHex(0x333333)];
        [self addSubview:_typeLBL];
        
        [_typeLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self).with.offset(10);
            make.top.mas_equalTo(self).with.offset(5);
            make.size.mas_equalTo(_typeLBL.bounds.size);
        }];
    }
    return _typeLBL;
}

- (CYLabel *)dateLBL{
    if (!_dateLBL) {
        _dateLBL = [[CYLabel alloc]initWithText:nil size:11 textcolor:ColorHex(0x888888)];
        [self addSubview:_dateLBL];
        
        [_dateLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_dateIMG.mas_right).with.offset(0);
            make.centerY.equalTo(_typeLBL);
            make.size.mas_equalTo(_dateLBL.bounds.size);
        }];
    }
    return _dateLBL;
}

- (UIImageView *)dateIMG{
    if (!_dateIMG) {
        _dateIMG = [[UIImageView alloc] init];
        [self addSubview:_dateIMG];
        
        [_dateIMG mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_typeLBL.mas_right).with.offset(10);
            make.size.mas_equalTo(CGSizeMake(10, 10));
            make.centerY.equalTo(_typeLBL);
        }];
    }
    return _dateIMG;
}

- (CYLabel *)timeLBL{
    if (!_timeLBL) {
        _timeLBL = [[CYLabel alloc]initWithText:_action_data_time size:11 textcolor:ColorHex(0x888888)];
        [self addSubview:_timeLBL];
        
        [_timeLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_dateLBL.mas_right).with.offset(5);
            make.centerY.equalTo(_typeLBL);
            make.size.mas_equalTo(_timeLBL.bounds.size);
        }];
    }
    return _timeLBL;
}

- (CYView *)greenView{
    if (!_greenView) {
        _greenView = [[CYView alloc]initWithColor:RGBA(30, 194, 130, 1)];
        _greenView.bounds = CGRectMake(0.0, 0.0, AUTO(30) * (_data.count + 1), 30);
        _greenView.layer.cornerRadius = _greenView.bounds.size.height / 2.0;
        _greenView.layer.masksToBounds = YES;
        _greenView.tag = 300;
        [self addSubview:_greenView];
        
        [_greenView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_typeLBL.mas_bottom).with.offset((100 - CGRectGetMaxY(_typeLBL.bounds) - 30) / 2.0 - 2);
            make.left.mas_equalTo(_typeLBL);
            make.size.mas_equalTo(_greenView.bounds.size);
        }];
    }
    return _greenView;
}

- (CYLabel *)sumLBL{
    if (!_sumLBL) {
        NSString *title = [NSString stringWithFormat:@"和值:%d",[_data sum]];
        _sumLBL = [[CYLabel alloc]initWithText:title size:13 textcolor:ColorHex(0x333333)];
        _sumLBL.tag = 301;
        [self addSubview:_sumLBL];
        
        [_sumLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_greenView);
            make.left.equalTo(_greenView.mas_right).with.offset(AUTO(30));
            make.size.mas_equalTo(_sumLBL.bounds.size);
        }];
    }
    return _sumLBL;
}


- (CYLabel *)tipsLBL{
    if (!_tipsLBL) {
        _tipsLBL = [[CYLabel alloc]initWithText:@"正在开奖..." size:15 textcolor:ColorHex(0x888888)];
        [self addSubview:_tipsLBL];
        
        [_tipsLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.left.equalTo(self).with.offset(10);
            make.size.mas_equalTo(_tipsLBL.bounds.size);
        }];
    }
    
    return _tipsLBL;
}
- (void)setName:(NSString *)name{
    if (_name != name)
        _name = name;
    self.typeLBL.text = name;
    [self updateTypeLBL];
    
}

- (void)setAction_no:(NSString *)action_no{
    if (_action_no != action_no)
        _action_no = action_no;
    self.dateIMG.image = [UIImage imageNamed:@"time"];
    self.dateLBL.text = [NSString getDateNumber:action_no];
    [self updateDateLBL];
    
}

- (void)setData:(NSArray *)data{
    if (_data != data)
        _data = data;
    [self clearSubview];
    [self setupSubview];
}

- (void)setAction_data_time:(NSString *)action_data_time{
    if (_action_data_time != action_data_time)
        _action_data_time = action_data_time;
    
    self.timeLBL.text = action_data_time;
    [self updateTimeLBL];
    
}

- (void)updateTypeLBL{
    
    [_typeLBL mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(_typeLBL.bounds.size);
    }];
    
}

- (void)updateDateLBL{
    
    [_dateLBL mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(_dateLBL.bounds.size);
    }];
    
}

- (void)updateTimeLBL{
    
    [_timeLBL mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(_timeLBL.bounds.size);
    }];
    
}

/*设置子视图的布局*/
- (void)setupSubview{
    if (!_data)
        self.tipsLBL.hidden = NO;
    else{
        if ([_name containsString:@"快三"]){
            self.greenView.hidden = self.sumLBL.hidden = NO;
            _sumLBL.text = [NSString stringWithFormat:@"和值:%d",[_data sum]];
            [_sumLBL mas_updateConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(_sumLBL.bounds.size);
            }];
        }
        for (int i = 0 ; i < _data.count; i++) {
            //快三
            if ([_name containsString:@"快三"]) {
                NSString *title = [NSString stringWithFormat:@"%dse",[_data[i] intValue]];
                self.greenView.hidden = self.sumLBL.hidden = NO;
                
                CYImageView *imageView = [[CYImageView alloc]initWithImageName:title size:CGSizeMake(25, 25) isRadius:NO];
                imageView.tag = 400 + i;
                [self.greenView addSubview:imageView];
                
                CGFloat offset_width = (_greenView.bounds.size.width - imageView.bounds.size.width * _data.count) / 4.0;
                [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerY.equalTo(_greenView);
                    make.left.equalTo(_greenView).with.offset(offset_width + (imageView.bounds.size.width + offset_width) * i);
                    make.size.mas_equalTo(imageView.bounds.size);
                }];
            }
            //其他彩种
            else {
                CGFloat radius = (_data.count < 10)?30:25;
                CGFloat offset_width = (_data.count < 10)?AUTO(15):AUTO(5);
                CGFloat font = (_data.count < 10)?14:11;
                CYLabel *label = [[CYLabel alloc]initWithText:_data[i] size:font weight:UIFontWeightBold];
                label.justSizeToFit = NO;
                label.backgroundColor = THEME_COLOR;
                label.textColor = [UIColor whiteColor];
                label.layer.cornerRadius = radius / 2.0;
                label.layer.masksToBounds = YES;
                label.textAlignment = NSTextAlignmentCenter;
                label.tag = 200 + i;
                [self addSubview:label];
                
                [label mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.mas_equalTo(_typeLBL.mas_bottom).with.offset((100 - CGRectGetMaxY(_typeLBL.bounds) - 30) / 2.0 - 2);
                    make.left.mas_equalTo(_typeLBL).with.offset((radius + offset_width) * i);
                    make.size.mas_equalTo(CGSizeMake(radius, radius));
                }];
            }
        }
    }
}

- (void)clearSubview{
    
    self.tipsLBL.hidden = YES;
    NSArray *array = [self subviews];
    for (int i = 0; i < array.count; i ++) {
        CYView *view = array[i];
        if (view.tag >= 200 && view.tag!= 300 && view.tag != 301)
            [view removeFromSuperview];
        else if (view.tag == 300 || view.tag == 301)
            view.hidden = YES;
    }
    
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
